package com.sinopec.basemodule.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sinopec.basemodule.BaseApplication;
import com.sinopec.basemodule.R;
import com.sinopec.basemodule.common.Logger;
import com.sinopec.basemodule.utils.NetUtil;

import java.util.List;

/**
 * @author lwj
 * @ClassName: MultiItemTypeAdapter
 * @Description:通用適配器
 */
public class MultiItemTypeAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected Context mContext;
    protected List<T> mDatas;

    protected ItemViewDelegateManager<T> mItemViewDelegateManager;
    protected OnItemClickListener mOnItemClickListener;

    public final static int VIEW_TYPE_EMPTY = 9999;


    public MultiItemTypeAdapter(Context context, List<T> datas) {
        mContext = context;
        mDatas = datas;
        mItemViewDelegateManager = new ItemViewDelegateManager<T>();
    }

    @Override
    public int getItemViewType(int position) {
        Logger.d("sglei-refresh", "getitemviewtype");
        if (mDatas.size() == 0)
            return VIEW_TYPE_EMPTY;
        if (!useItemViewDelegateManager())
            return super.getItemViewType(position);
        return mItemViewDelegateManager.getItemViewType(mDatas.get(position), position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Logger.d("sglei-refresh", "viewType = " + viewType);
        ItemViewDelegate itemViewDelegate = mItemViewDelegateManager.getItemViewDelegate(viewType);
        if (viewType == VIEW_TYPE_EMPTY) {
            return new RecyclerView.ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.view_empty, parent, false)) {
            };
        } else {
            CustomViewHolder holder = CustomViewHolder.createViewHolder(mContext, parent, itemViewDelegate.getItemViewLayoutId());
            onViewHolderCreated(holder, holder.getConvertView());
            setListener(parent, holder, viewType);
            return holder;
        }
    }

    public void onViewHolderCreated(RecyclerView.ViewHolder holder, View itemView) {

    }

    public void convert(RecyclerView.ViewHolder holder, T t) {
        if (holder instanceof CustomViewHolder)
            mItemViewDelegateManager.convert((CustomViewHolder) holder, t, holder.getAdapterPosition());
    }

    protected boolean isEnabled(int viewType) {
        return true;
    }


    protected void setListener(final ViewGroup parent, final CustomViewHolder viewHolder, int viewType) {
        if (!isEnabled(viewType)) return;
        viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    int position = viewHolder.getAdapterPosition();
                    mOnItemClickListener.onItemClick(v, viewHolder, position);
                }
            }
        });

        viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListener != null) {
                    int position = viewHolder.getAdapterPosition();
                    return mOnItemClickListener.onItemLongClick(v, viewHolder, position);
                }
                return false;
            }
        });
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Logger.d("sglei-refresh", "onBindViewHolder = " + position);
        if (holder instanceof CustomViewHolder) {
            convert(holder, mDatas.get(position));
        } else {
            ImageView imageView = holder.itemView.findViewById(R.id.imageView);
            if (NetUtil.isNetworkAvailable(BaseApplication.getInstance())) {
                imageView.setImageResource(R.drawable.no_data_img);
            } else {
                imageView.setImageResource(R.drawable.no_net_img);
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads.size() == 0) {
            onBindViewHolder(holder, position);
        } else if (holder instanceof CustomViewHolder) {
            convert((CustomViewHolder) holder, mDatas.get(position), payloads);
        }
    }

    public void convert(CustomViewHolder holder, T t, List<Object> payloads) {
        mItemViewDelegateManager.convert(holder, t, holder.getAdapterPosition(), payloads);
    }

    @Override
    public int getItemCount() {
        return mDatas.size() > 0 ? mDatas.size() : 1;
    }


    public List<T> getDatas() {
        return mDatas;
    }

    public MultiItemTypeAdapter addItemViewDelegate(ItemViewDelegate<T> itemViewDelegate) {
        mItemViewDelegateManager.addDelegate(itemViewDelegate);
        return this;
    }

    public MultiItemTypeAdapter addItemViewDelegate(int viewType, ItemViewDelegate<T> itemViewDelegate) {
        mItemViewDelegateManager.addDelegate(viewType, itemViewDelegate);
        return this;
    }

    protected boolean useItemViewDelegateManager() {
        return mItemViewDelegateManager.getItemViewDelegateCount() > 0;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, RecyclerView.ViewHolder holder, int position);

        boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
